<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['lims:data_entry:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lims:data_entry:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lims:data_entry:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['lims:data_entry:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="data_entryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="物质的量(g)" align="center" prop="quality" />
      <el-table-column label="无机移液量(ml)" align="center" prop="inorganic" />
      <el-table-column label="有机移液量(ml)" align="center" prop="organic" />
      <el-table-column label="制样量--测钛(ml)" align="center" prop="samplePreparation" />
      <el-table-column label="镁体积(ml)" align="center" prop="magnesium" />
      <el-table-column label="氯体积(ml)" align="center" prop="chlorine" />
      <el-table-column label="紫外钛吸光" align="center" prop="titanium" />
      <el-table-column label="钛含量(%)" align="center" prop="tiContent" />
      <el-table-column label="镁含量(%)" align="center" prop="mgContent" />
      <el-table-column label="氯含量(%)" align="center" prop="clContent" />
      <el-table-column label="己烷保留时间" align="center" prop="hexaneTime" />
      <el-table-column label="己烷峰面积" align="center" prop="hexaneArea" />
      <el-table-column label="辛烷保留时间" align="center" prop="octaneTime" />
      <el-table-column label="辛烷峰面积" align="center" prop="octaneArea" />
      <el-table-column label="电子保留时间" align="center" prop="electronTime" />
      <el-table-column label="电子峰面积" align="center" prop="electronArea" />
      <el-table-column label="戊酯保留时间" align="center" prop="amylEsterTime" />
      <el-table-column label="戊酯峰面积" align="center" prop="amylEsterArea" />
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="data_entry_type" :value="scope.row.status"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['lims:data_entry:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['lims:data_entry:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据录入对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
      <el-form ref="data_entryRef" :model="form" :rules="rules" label-width="150px">
        <el-card style="box-shadow: 0 2 8 rgba(0, 0, 0, 0.1); margin-bottom: 20px;">
          <template #header ><span style="font-weight: bold; font-size: 18px;">前处理数据报告及需采集数据</span></template>
          <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物质的量(g)" prop="quality">
              <el-input v-model="form.quality" placeholder="请输入物质的量(g)" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="无机移液量(ml)" prop="inorganic">
              <el-input v-model="form.inorganic" placeholder="请输入无机移液量(ml)" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="有机移液量(ml)" prop="organic">
              <el-input v-model="form.organic" placeholder="请输入有机移液量(ml)" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制样量--测钛(ml)" prop="samplePreparation">
              <el-input v-model="form.samplePreparation" placeholder="请输入制样量--测钛(ml)" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        </el-card>
        <el-card style="box-shadow: 0 2 8 rgba(0, 0, 0, 0.1); margin-bottom: 20px;">
          <template #header><span style="font-weight: bold; font-size: 18px;">电位滴定-UV联用报告及需采集数据</span></template>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="镁体积(ml)" prop="magnesium">
                <el-input v-model="form.magnesium" placeholder="请输入镁体积(ml)" type="number" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="氯体积(ml)" prop="chlorine">
                <el-input v-model="form.chlorine" placeholder="请输入氯体积(ml)" type="number" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="紫外钛吸光" prop="titanium">
                <el-input v-model="form.titanium" placeholder="请输入紫外钛吸光" type="number" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-card style="box-shadow: 0 2 8 rgba(0, 0, 0, 0.1); margin-bottom: 20px;">
          <template #header><span style="font-weight: bold; font-size: 18px;">Ti-Mg-Cl 含量</span></template>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="钛含量(%)" prop="magnesium" style="display: flex;">
                <el-input v-model="form.tiContent" placeholder="待计算" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="镁含量(%)" prop="magnesium">
                <el-input v-model="form.mgContent" placeholder="待计算" disabled />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="氯含量(%)" prop="magnesium">
                <el-input v-model="form.mgContent" placeholder="待计算" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <div style="width: 100%; display: flex; align-items: center;">
                <el-button type="primary" style="margin-left: 150px;">一键测算</el-button>
              </div>
            </el-col>

          </el-row>
        </el-card>
        <el-card style="box-shadow: 0 2 8 rgba(0, 0, 0, 0.1); margin-bottom: 20px;">
          <template #header><span style="font-weight: bold; font-size: 18px;">气相色谱报告及需采集数据</span></template>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="己烷保留时间" prop="hexaneTime">
                <el-input v-model="form.hexaneTime" placeholder="请输入己烷保留时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="己烷峰面积" prop="hexaneArea">
                <el-input v-model="form.hexaneArea" placeholder="请输入己烷峰面积" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="辛烷保留时间" prop="octaneTime">
                <el-input v-model="form.octaneTime" placeholder="请输入辛烷保留时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="辛烷峰面积" prop="octaneArea">
                <el-input v-model="form.octaneArea" placeholder="请输入辛烷峰面积" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="电子保留时间" prop="electronTime">
                <el-input v-model="form.electronTime" placeholder="请输入电子保留时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电子峰面积" prop="electronArea">
                <el-input v-model="form.electronArea" placeholder="请输入电子峰面积" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="戊酯保留时间" prop="amylEsterTime">
                <el-input v-model="form.amylEsterTime" placeholder="请输入戊酯保留时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="戊酯峰面积" prop="amylEsterArea">
                <el-input v-model="form.amylEsterArea" placeholder="请输入戊酯峰面积" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <!-- <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in data_entry_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Data_entry">
import { listData_entry, getData_entry, delData_entry, addData_entry, updateData_entry } from "@/api/data_entry/data_entry.js";

const { proxy } = getCurrentInstance();
const { data_entry_type } = proxy.useDict('data_entry_type');

const data_entryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    status: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询数据录入列表 */
function getList() {
  loading.value = true;
  listData_entry(queryParams.value).then(response => {
    data_entryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    quality: null,
    inorganic: null,
    organic: null,
    samplePreparation: null,
    magnesium: null,
    chlorine: null,
    titanium: null,
    hexaneTime: null,
    hexaneArea: null,
    octaneTime: null,
    octaneArea: null,
    electronTime: null,
    electronArea: null,
    amylEsterTime: null,
    amylEsterArea: null,
    status: null
  };
  proxy.resetForm("data_entryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据录入";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getData_entry(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改数据录入";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["data_entryRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateData_entry(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addData_entry(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除数据录入编号为"' + _ids + '"的数据项？').then(function() {
    return delData_entry(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('lims/data_entry/export', {
    ...queryParams.value
  }, `data_entry_${new Date().getTime()}.xlsx`)
}

getList();
</script>
